package com.core.interceptor;

import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.common.action.LoginAction;
import com.common.util.StringToken;
import com.core.ssh2.AbstractActionSupport;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class LoginInterceptor extends AbstractInterceptor {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private Log logger = LogFactory.getLog(this.getClass());
	
	private Long module;

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		AbstractActionSupport action = (AbstractActionSupport) invocation
				.getAction();
		logger.info("Request file or action - " + action);

		// 登陆页面不做拦截
		if (invocation.getAction() instanceof LoginAction) {
			return invocation.invoke();
		}

//		module = Long.valueOf(action.getRequest().getParameter("module"));
//		logger.info("Interceptor get the module is - " + module);
//		// 非法的模块访问
//		if (module == null) {
//			return Action.LOGIN;
//		}

		ActionContext ctx = invocation.getInvocationContext();
		Map session = ctx.getSession();
		Object obj = session.get(StringToken.CURRENT_LOGIN);
		// 没有登陆的访问
		if (obj == null) {
			return Action.LOGIN;
		}

//		// 管理员没有权限的访问
//		if (obj instanceof Admin) {
//			if (module.equals(StringToken.USERSELF_MANAGE_INT)) {
//				return Action.LOGIN;
//			}
//			Admin admin = (Admin) obj;
//			// 超级管理员不受权限限制
//			if (admin.getId().equals(StringToken.SUPPER_ADMIN)) {
//				return invocation.invoke();
//			}
//			List rights = adminService.findAdminRights(admin);
//			boolean hasRights = false;
//			for (Object temp : rights) {
//				AdmRights ar = (AdmRights) temp;
//				if (module.equals(ar.getModuleId())||module.equals(StringToken.SPECIAL_MODULE_INT)) {
//					hasRights = true;
//					break;
//				}
//			}
//			if (!hasRights) {
//				return Action.LOGIN;
//			}
//		}

//		String modules = StringToken.MODULES;
//		// 访问不存在的模块
//		if (modules.indexOf(module.toString()) == -1) {
//			return Action.LOGIN;
//		}
		return invocation.invoke();
	}

	public Long getModule() {
		return module;
	}

	public void setModule(Long module) {
		this.module = module;
	}
}
